<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/16
 * Time: 14:01
 */
namespace app\api\model;
use think\Request;
use think\Db;
use app\api\model\WeightedRoundRobin;
use app\store\model\jacket\Setting as SettingModel;
class Jocket extends BaseModel
{
    protected $bmarket;
    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->bmarket=db::name('jacket_bmarket');
        $this->settingModel=new SettingModel();
    }

    public function  getJocketLast(){
        if(!$this->validata()){ //检测开盘时间
            return ['message'=>'未到开盘时间','status'=>0];
        }
        $this->calcuLation(); //处理历史数据
        return ['message'=>'未到开盘时间','status'=>1];
    }
    //检测开盘时间
    private  function  validata(){
        $data = $this->settingModel->detail();
        $nowTime = date('H:i:s');
        if($data['open_time']>$nowTime || $data['down_time']<$nowTime){
            return false;
        }
        return true;
    }
    //处理历史数据
    private function calcuLation(){
        $bmarketList=$this->bmarket
            ->where(array('state'=>0,'run_time'=>array('lt',time())))
            ->select();
        foreach($bmarketList as $key=>$value){
            $this->checkLation($value['id']);
        }
    }
    //清算倍数
    public  function updatePoint($id){
        $config = $this->settingModel->detail();
        $bmarket=db::name('jacket_bmarket')->where('id',$id)->find();
        $data=Db::name('jacket_point')->where(array('b_id'=>$id,'state'=>0))->select();//投注人数
        foreach ($data as $key=>$value){
            if($bmarket['b_multiple']>$value['final_multiple'] && $value['final_multiple']){
                $pageParam['state']=2;
                $pageParam['pattern']=1;
                $pageParam['final_multiple']=$value['final_multiple'];
                db::startTrans();
                if($pageParam['state']==2){
                    $pingtaiPrice=$value['price'] * $pageParam['final_multiple']*$config['profit'];//平台收益
                    $return_price=$value['price'] * $pageParam['final_multiple']-$pingtaiPrice;
                    $profit=$return_price-$value['price'];
                }
                $data=array(
                    'b_multiple'=>$bmarket['b_multiple'],
                    'final_multiple'=>$pageParam['final_multiple'],
                    'return_price'=>$return_price,
                    'profit'=>$profit,
                    'state'=>$pageParam['state'],
                    'pattern'=>$pageParam['pattern']
                );
                $isok=$point=db::name('jacket_point')->where(array('id'=>$value['id']))->update($data);

                $platform=$point=db::name('platform')->where(array('id'=>1))->setInc('platform_point',$pingtaiPrice);//平台收益

                $balcnce=db::name('balance')->where('user_id',$value['uid'])->setInc('qc',$return_price);

                if($isok && $balcnce && $platform) {
                    db::commit();
                }
            }
        }
    }
    /**
     * 清算每一期的奖池盈利情况
     * @param $id期数id
     */
    private  function checkLation($id){
        $where['b_id']=$id;
        $pointCount= Db::name('jacket_point')->where($where)->count();//投注人数
        $this->updatePoint($id);
        db::startTrans();
        $bmarket=db::name('jacket_bmarket')->where('id',$id)->find();
        db::name('jacket_point')->where(['b_id'=>$id,'state'=>0])->update(array('state'=>1,'b_multiple'=>$bmarket['b_multiple']));
        if($pointCount){
            $where['b_id']=$id;
            $return_price=db::name('jacket_point')->where($where)->sum('return_price');//此期总返钱
            $config = $this->settingModel->detail();
            if($return_price>$bmarket['jackpot']){
                $banList=db::name('banker')->count();
                $price=$return_price-$bmarket['jackpot'];
                if($banList){
                    $sql="UPDATE `yoshop_banker`  SET `profit` =profit-(shares*".$price*$config['zjks'].")";
                    $banker=db::execute($sql);
                    if($banker===false){
                        db::rollback();
                        AjaxReturn('0','失败');exit;
                    }
                    $beilv=1-$config['zjks'];
                    $jiangchi="UPDATE `yoshop_bnumber`  SET `number` =`number`-(".$price*$beilv.")";
                }else{
                    $jiangchi="UPDATE `yoshop_bnumber`  SET `number` =`number`-(".$price.")";
                }


            }else{
                $banList=db::name('banker')->count();
                $price=$jocket['jackpot']-$return_price;
                if($banList){
                    $sql="UPDATE `yoshop_banker`  SET `profit` =profit+(shares*".$price*$config['zjyl'].")";
                    $beilv=1-$config['zjyl'];
                    $jiangchi="UPDATE `yoshop_bnumber`  SET `number` =`number`+(".$price*$beilv.")";
                    $banker=db::execute($sql);
                    if($banker===false){
                        db::rollback();
                        AjaxReturn('0','失败');exit;
                    }
                }else{
                    $jiangchi="UPDATE `yoshop_bnumber`  SET `number` =`number`+(".$price.")";
                }
            }
            $jiangchi=db::execute($jiangchi);
            if($jiangchi===false){
                db::rollback();
                AjaxReturn('0','失败');exit;
            }
        }
        db::name('jacket_bmarket')->where('id',$id)->update(array('state'=>1));
        db::commit();
    }

}